package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 套餐管理组
 **/
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage( QueryPageBean queryPageBean) {
        log.info("[套餐管理-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = setmealService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true,MessageConst.ACTION_SUCCESS, pageResult);
    }
    /**
     * 新增
     * 1、rpc调用完成新增业务
     * 2、返回结果
     * @param
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkgroupIds){
        log.info("[检查组-新增]data:{}",setmeal);
        setmealService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConst.ADD_CHECKITEM_SUCCESS);
    }


    /**
     * 文件上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        log.info("文件上传：{}", imgFile);
        //原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String name = UUID.randomUUID().toString() + extension;

        try {
            String filePath = aliOssUtil.upload(imgFile.getBytes(), name);
            return new Result(true,MessageConst.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("文件上传失败");
        }
        return new Result(false,MessageConst.PIC_UPLOAD_SUCCESS);
    }

}
